package com.platform.controller;


import com.platform.entity.LotteryEntity;
import com.platform.entity.LotterySignEntity;
import com.platform.service.LotteryRecordService;
import com.platform.service.LotteryService;
import com.platform.service.LotterySignService;
import com.platform.service.LotteryWagerService;
import com.platform.utils.CharUtil;
import com.platform.utils.DateUtils;

import lombok.extern.slf4j.Slf4j;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.math.BigDecimal;
import java.util.*;
import java.util.stream.Collectors;

@Slf4j
@EnableScheduling
@Component("lotteryTask")
public class LotteryTask {
    private Logger logger = LoggerFactory.getLogger(getClass());

    @Autowired
    private LotteryRecordService lotteryRecordService;
    @Autowired
    private LotteryService lotteryService;
    @Autowired
    private LotterySignService lotterySignService;

   /* @Scheduled(cron = "0 0 9 * * ? ")*/
    public void operlottersignbyday() {

        Map<String, Object> map = new HashMap<>();
        map.put("isyestoday", 1);
        List<LotterySignEntity>  lotterySignEntitys= lotterySignService.queryList(map);
        if(lotterySignEntitys.size()<=0)
            return;
        BigDecimal nosignmoney = new BigDecimal(0);
        Integer signcount = 0;
        for (LotterySignEntity lotterySignEntity : lotterySignEntitys) {
            if(lotterySignEntity.getStatus()==0)
                nosignmoney = nosignmoney.add(lotterySignEntity.getSpend());
            else if(lotterySignEntity.getStatus()==1)
                signcount++;
        }
        for (LotterySignEntity lotterySignEntity : lotterySignEntitys) {
            if(lotterySignEntity.getStatus()==0) {
                lotterySignEntity.setStatus(2);
                lotterySignService.update(lotterySignEntity);
            }
            else if(lotterySignEntity.getStatus()==1)
            {
                map = new HashMap<>();
                map.put("level", 1);
                map.put("type", 4);
                List<LotteryEntity> le = lotteryService.queryList(map);
                if(le==null&&le.size()<=0)
                    continue;
                LotteryEntity lotteryEntity = le.get(0);
                BigDecimal price = new BigDecimal(0);
                if(nosignmoney.compareTo(new BigDecimal(0))<=0)
                    price=lotterySignEntity.getSpend();
                else
                    price=lotterySignEntity.getSpend().add(nosignmoney.multiply(lotteryEntity.getPrice()).divide(new BigDecimal(signcount)));
                lotteryRecordService.savesignrecordinfo(lotteryEntity,lotterySignEntity.getUid(),price,lotterySignEntity.getSpend());
            }
        }
    }

    /*@Scheduled(cron = "0 0 12 * * ? ")*/
    public void operlottersignbyweek() {
        Integer weeknum = getWeekOfDate(new Date());
        if(weeknum==7) {
            Map<String, Object> map = new HashMap<>();
            map.put("isweekly", 1);
            List<LotterySignEntity> lotterySignEntitys = lotterySignService.queryList(map);
            if (lotterySignEntitys == null || lotterySignEntitys.size() <= 0)
                return;
            BigDecimal nosignmoney = new BigDecimal(0);

            for (LotterySignEntity lotterySignEntity : lotterySignEntitys) {
                if (lotterySignEntity.getStatus() == 2)
                    nosignmoney = nosignmoney.add(lotterySignEntity.getSpend());
            }
            map = new HashMap<>();
            map.put("isweeklysign", 1);
            List<LotterySignEntity> weeklylotterySigns = lotterySignService.queryList(map);
            if (weeklylotterySigns == null || weeklylotterySigns.size() <= 0)
                return;
            List<Long> userIds = weeklylotterySigns.stream().map(u -> u.getUid()).collect(Collectors.toList()).stream().distinct().collect(Collectors.toList());
            for (Long uid : userIds) {
                map = new HashMap<>();
                map.put("level", 2);//1日奖励，2周奖励
                map.put("type", 4);
                List<LotteryEntity> le = lotteryService.queryList(map);
                if (le == null && le.size() <= 0)
                    continue;
                LotteryEntity lotteryEntity = le.get(0);
                BigDecimal price = new BigDecimal(0);
                if (nosignmoney.compareTo(new BigDecimal(0)) > 0) {
                    price = nosignmoney.multiply(lotteryEntity.getPrice()).divide(new BigDecimal(userIds.size()));
                    lotteryRecordService.savesignrecordinfo(lotteryEntity, uid, price, new BigDecimal(10));
                }
            }
        }
    }

    public static Integer getWeekOfDate(Date dt) {
        Integer[] weekDays = {7, 1, 2, 3, 4, 5, 6};
        Calendar cal = Calendar.getInstance();
        cal.setTime(dt);

        int w = cal.get(Calendar.DAY_OF_WEEK) - 1;
        if (w < 0)
            w = 0;

        return weekDays[w];
    }
}
